home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.05 May 90 / virusCheck Code / virusCheck.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-05  |  4.6 KB  |  102 lines  |  [TEXT/KAHL]

  1. /*    virusCheck.h -- Functions for self-diagnosis of virus infections.
  2.             Copyright © 1989 by Michael S. Morton.
  3.             Special thanks to John Norstad and Andrew Levin for advice.
  4.             You may copy, alter, use, and distribute these routines if you
  5.             leave this file unchanged up to this line.
  6.  
  7.         Think C version.
  8.  
  9.         Notes:
  10.         -----
  11.         •    You are STRONGLY urged to make non-functional changes to both C
  12.             functions, to discourage the invention of viruses which recognize
  13.             this code and disable it.  Specifically:
  14.                 - all parameters and local variables are now declared “register”;
  15.                     delete the ‘register’ keyword for randomly-chosen variables
  16.                 -    declare your own variables and pepper the code with assignments
  17.                     involving them -- “a = b+c/d*e+f”.  (Be sure to avoid division by 0.)
  18.                 - reorder pairs of lines which are preceded by this comment:
  19.                             You can swap the order of the next two lines
  20.                 -    test your application (see below) after all these changes
  21.                 -    remember that you can call this function from more than one place
  22.                     in your application
  23.  
  24.         •    To calibrate your application:
  25.                 -    set the application’s calls to vResCheck or vCodeCheck to pass “1”
  26.                     for the “report” parameter
  27.                 -    build a standalone, double-clickable application
  28.                 -    make sure that you have a debugger installed which can intercept
  29.                     calls to DebugStr () -- MacsBug or TMON will do
  30.                 -    run the application; if you get messages of the form:
  31.                         Got count CC for resource type '<type>', instead of <expected>
  32.                         Got length LL for resource type '<type>', instead of <expected>
  33.                     then change the arguments in your calls to vCodeCheck() and
  34.                     vResCheck() to pass CC for count and LL for long
  35.  
  36.         •    To test your application’s virus-detection:
  37.                 -    calibrate it as above
  38.                 -    change the application’s calls to pass “-1” for reporting
  39.                 -    build a standalone, double-clickable application
  40.                 -    use ResEdit to add a CODE resource from anywhere to your application
  41.                 -    launch the application and make sure it detects and reports infection
  42.                 -    delete the added CODE resource or build the application again
  43.  
  44.         •    Both of these C functions require EITHER a Mac Plus or 512KE or later,
  45.             OR System file 3.2 or later (for the “one deep” resource calls).
  46.             The application must check this before calling these.
  47.             N.B.: I’m not 100% sure that System 3.2 will work on 128K/512K ROM;
  48.                         please try it if you expect your application to work on this
  49.                         configuration.
  50.  
  51.         •    The “report” parameter takes 1 and -1, not 1 and 0, because many
  52.             compilers will compile a parameter 0 in less space than a parameter 1.
  53.  
  54.         •    If these functions encounter an unexpected error, they act
  55.             conservatively and assume there’s an infection.
  56.  
  57.         •    If you’re working under Think C, the checksum will be different
  58.             depending on whether your application is running as a project or
  59.             as a standalone application.
  60.  
  61.             You may want to use this technique (invented by David Oster, I think)
  62.             which tests whether the Think C environment is present.  It relies on
  63.             the fact that your project’s resource file is the current resource file
  64.             when you start up, and it contains no CODE resources.
  65.                         if (Count1Resources ('CODE'))        -- are we standalone or project?
  66.                             if (vCodeCheck (…))                        -- standalone: do the check
  67.                                 { … }                                                -- check failed: report virus
  68.             For this to work, you must have a resource file “project.rsrc”.
  69.  
  70.         •    In certain obscure cases, you may find that changing the
  71.             arguments changes the code length.  For example, changing:
  72.                         vCodeCheck (3, 0L, 1);
  73.             to
  74.                         vCodeCheck (3, 12345L, 1);
  75.             will do this.  If this is a problem, move the constant out of the
  76.             code with something like:
  77.                 static long expected = 12345L;
  78.                 vCodeCheck (3, expected, 1);
  79. */
  80.  
  81. #ifndef _virusCheck_                                        /* already seen this */
  82. #define _virusCheck_                                        /* yes: don’t define it again */
  83.  
  84. /*    vCodeCheck -- Check for apparent alteration of CODE resources.  Return
  85.         TRUE if the count/length do NOT match, meaning an apparent infection.
  86. */
  87. extern Boolean vCodeCheck (
  88.     short expectedCount,                                    /* expected number of CODEs */
  89.     long expectedLen,                                            /* expected total size of CODEs */
  90.     short report);                                                /* >0 => report errors to developer */
  91.  
  92. /*    vResCheck -- Check for apparent alteration of resources.  Return TRUE if
  93.         the count/length do NOT match, meaning an apparent infection.
  94. */
  95. extern Boolean vResCheck (
  96.     ResType type,                                                    /* type of resource to sum */
  97.     short expectedCount,                                    /* expected number of resources */
  98.     long expectedLen,                                            /* expected total size of resources */
  99.     short report);                                                /* >0 => report errors to developer */
  100.  
  101. #endif _virusCheck_
  102.